﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PhantomRealm.Common.Players;
using Terraria;
using Terraria.GameContent;
using Terraria.Graphics.Effects;

namespace PhantomRealm.Content.Skies.Overlays;

public class AuroraOverlay(EffectPriority priority = EffectPriority.High, RenderLayers layer = RenderLayers.Sky) : Overlay(priority, layer)
{
    private readonly int BG_TOP_OFFSET = 460;
    private readonly int CAMERA_OFFSET_MULT = 700;
    private float time;
    public const int BASIC = 0;
    public const int COUNT = 1;
    public override void Activate(Vector2 position, params object[] args) => Mode = OverlayMode.FadeIn;
    public override void Deactivate(params object[] args) => Mode = OverlayMode.FadeOut;
    public override bool IsVisible() => !Main.gameMenu;
    public override void Update(GameTime gameTime) => time = (float)gameTime.TotalGameTime.TotalSeconds * 3f;
    public override void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.End();
        DrawAurora(spriteBatch);
        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone, null, Main.GameViewMatrix.TransformationMatrix);
    }
    private void DrawAurora(SpriteBatch spriteBatch)
    {
        if (PhantomRealm.AuroraEffect != null)
        {
            PhantomRealm.AuroraEffect.Parameters["noiseTexture"].SetValue(PhantomRealm.Noise);
            int bgTop = (int)(-Main.screenPosition.Y / (Main.worldSurface * 16.0 - 550.0) * 200.0);
            float percent = Main.screenPosition.Y / ((float)Main.worldSurface * 16f);
            float bonus = 0;
            if (Main.screenPosition.Y < 1600)
            {
                bonus = (1600f - Main.screenPosition.Y) * 0.036f;
                bonus *= bonus;
            }
            int basePoint = (int)(bgTop + BG_TOP_OFFSET - percent * (CAMERA_OFFSET_MULT - bonus));
            const int Height = 600;
            if (ActiveAurora(BASIC, out float i))
            {
                DrawSingularAurora(spriteBatch, basePoint, 10, Height, new Color(0, 143, 233), new Color(0, 255, 255), 0.002f, 1f, 0.05f);
                DrawSingularAurora(spriteBatch, basePoint, 0, Height, new Color(9, 231, 167), new Color(0, 255, 100), 0.003f, 1f, 0.2f);
            }
            if (!Main.LocalPlayer.GetModPlayer<AuroraPlayer>().InLocalAurora)
            {
                Deactivate();
            }
            static bool ActiveAurora(int ID, out float intensity)
            {
                intensity = Main.LocalPlayer.GetModPlayer<AuroraPlayer>().GetAuroraIntensity(ID);
                return intensity > 0;
            }
        }
    }
    private void DrawSingularAurora(SpriteBatch spriteBatch, int basePoint, int yOffset, int height, Color topColor, Color bottomColor, float speed, float opacity, float randY)
    {
        spriteBatch.Begin(SpriteSortMode.Immediate, null);
        PhantomRealm.AuroraEffect.Parameters["time"].SetValue(time + (int)(randY * 1000));
        PhantomRealm.AuroraEffect.Parameters["yCoord"].SetValue(randY);
        PhantomRealm.AuroraEffect.Parameters["colour1"].SetValue(topColor.ToVector3());
        PhantomRealm.AuroraEffect.Parameters["colour2"].SetValue(bottomColor.ToVector3());
        PhantomRealm.AuroraEffect.Parameters["movement"].SetValue(speed);
        PhantomRealm.AuroraEffect.Parameters["opacity"].SetValue(opacity * Opacity);
        PhantomRealm.AuroraEffect.CurrentTechnique.Passes[0].Apply();
        spriteBatch.Draw(TextureAssets.BlackTile.Value, new Rectangle(0, basePoint + yOffset, Main.screenWidth, height), Color.White);
        spriteBatch.End();
    }
}
